<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/* MYSQL::getData('id,name,prefix', 'location', array('parent_id' => config_item('loc_default')), 30, 0, array('name' => 'ASC'));*/
function getSQLData($select,$table,$where,$limit=1,$offset=0,$order=null){

	$ci =& get_instance();

	if(isset($select) && !empty($select)){
		$ci->db->select("$select");
	}
	if(isset($limit) && !empty($limit)){
		if(isset($offset) && !empty($offset)){
			$ci->db->limit($limit,$offset);
		}
		else{
			$ci->db->limit($limit);
		}
	}
	if(isset($where) && !empty($where)){
		foreach ($where as $key => $value) {
			$ci->db->where($key,$value);
		}
	}
	if(isset($order) && !empty($order)){
		foreach ($order as $key => $value) {
			$ci->db->order_by($key,$value);
		}
	}else{
		$ci->db->order_by('id', 'ASC');
	}

	$query = $ci->db->get("$table");
	$result = $query->result_array();
	//return $result;


	if(isset($result) && !empty($result)){

		if( count($result) > 1){
			return $result;
		}else{
			return $result[0];
		}
	}

}

function seoUrl($strUrl = '', $case = FALSE )
{
	$url = url_title($strUrl,'-',$case);
	return $url;
}

function getID($strUrl){
	if(!$strUrl){
		return 0;
	}

	if($strUrl){
		$len=strlen($strUrl);
		$pos=strrpos($strUrl, '-');
		$id= substr($strUrl,$pos+1);
		//$id= substr($id,0,1);
		return $id;
	}

}


function location_text(){

	$CI = get_instance();
	$CI->load->model('location_model');

	$locSESS_ID = get_session('tmpLOC');

	if($locSESS_ID){
		$loc = $CI->location_model->getPK($locSESS_ID);
		$loc = $loc['name'];
	}else{
		$loc = config_item('country');
	}

	return $loc;
}


function location($loc = '',$url_title = FALSE){

	if($loc == ''){
		$CI = get_instance();
		$CI->load->model('location_model');

		$locSESS_ID = get_session('tmpLOC');

		if($locSESS_ID){
			$loc = $CI->location_model->getPK($locSESS_ID);
			$loc = $loc['name'];
		}else{
			$loc = config_item('country');
		}
	}
	
	if($url_title == FALSE) return $loc;
	if($url_title == TRUE) return seoUrl($loc);
}

function getPK($tbl='',$pKey=0){
	$ci =& get_instance();
	$sql = "select * from $tbl where id = $pKey limit 1";
	$result = $ci->db->query($sql)->result_array();

	if($result){
		return $result[0];
	}
}

function getSlugID($tbl='',$slug=''){
	$ci =& get_instance();
	$sql = "select id from $tbl where slug = '$slug' limit 1";
	$result = $ci->db->query($sql)->row();

	if($result){
		return $result->id;
	}else{
		return 0;
	}
}

function clear_session($item='',$value=''){
	$ci =& get_instance();

	$ci->session->unset_userdata($item, $value);

	//$ci->session->set_userdata[$item] = '';
	//$ci->session->set_userdata[$item] = $value;
	//$ci->session->sess_write();
	//return true;
}


function write_session($item='',$value=''){
	$ci =& get_instance();
	$ci->session->set_userdata($item, $value);
}

function get_session($item=null) {
	$ci =& get_instance();
	$d = 0;
	if(isset($ci->session) && $ci->session->userdata($item)){
		$d= $ci->session->userdata($item);
	}

	return $d;

}
function get_ip() {
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

function setting($name='') {

	$ci =& get_instance();

	if (!$ci->db->table_exists('settings')) {
		print "Error! Can't Load Setting Data..";
		return FALSE;
	}

	if (empty($name)) {
		return $name;
	} else {
		$ci->db->select($name)->from('web_settings');
		$query = $ci->db->get();
		if ($query && $query->num_rows() > 0) {
			$row = $query->row();
			return $row->$name;
		}
		return '';
	}
}


